package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommendService commendService;

    /**
     * 获取当前游记用于编辑回显
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Travel getById(@PathVariable Long id) {
        return travelService.getById(id);
    }

    /**
     * 编辑游记
     *
     * @param travel
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public JSONResult update(Travel travel) {
        JSONResult result = new JSONResult();
        try {
            travelService.update(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * app查看所有公开发布游记
     *
     * @param qo
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public PageInfo query(TravelQueryObject qo) {
        //查询已公开的
        qo.setIsPublic(true);
        //查询已发布的
        qo.setState(Travel.STATE_RELEASE);
        //安装发布时间倒序
        qo.setOrderBy("t.releaseTime desc");
        return travelService.queryForList(qo);
    }

    /**
     * app每周推荐和每月推荐
     * @param qo
     * @return
     */
    @RequestMapping(value = "/commends", method = RequestMethod.GET)
    public PageInfo queryCommend(TravelCommendQueryObject qo) {
        //安装推荐时间倒序排列
        qo.setOrderBy("tc.schedule desc");
        return commendService.queryForApp(qo);
    }
}
